package com.txby.dplus.identify.service;

import com.qiniu.util.Json;
import com.txby.dplus.common.annomapper.MybatisBaseDaoCRUD;
import com.txby.dplus.common.service.BaseService;
import com.txby.dplus.common.utils.util.RestClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.*;


public class SyncDataService {
    private static final Logger log = LoggerFactory.getLogger(SyncDataService.class);
    private String url = "http://192.168.2.84:8998/tzData/getCmDatas";
    private String startTime = "2017-11-18 10:46:17";
    private MybatisBaseDaoCRUD mBaseDao=new MybatisBaseDaoCRUD();

    public void startLoop() {
        Timer timer = new Timer();
        // 马上执行任务,每隔30000ms执行一次
        timer.scheduleAtFixedRate(new MyTask(), new Date(), 30000);
    }

    // 定时任务,这是一个线程
    class MyTask extends TimerTask {
        @Override
        public void run() {
            getCmDatas();
        }
    }

    private boolean getCmDatas() {
        try {
            //首先根据时间获取数据
            String sql = "select * from cm where checkdate>='" + startTime + "' order by checkdate desc";
            List<Map> list = mBaseDao.findBySql(sql, Map.class, null);
            log.info(list.toString());
            startTime = list.get(0).get("checkdate").toString();
            log.info(startTime);
            MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
            map.add("list", list);
            ResponseEntity<Boolean> response = RestClientUtil.getInstace().restTemplate.postForEntity(url, map, Boolean.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
